package com.loop.pdalib;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;

import com.sh_rw.label.LabelPerser;
import com.sh_rw.label.bean.BaseLabel;
import com.sh_rw.label.bean.BasicParam;
import com.sh_rw.label.bean.RFBean;
import com.sh_rw.label.common.black.BlackHandler;
import com.sh_rw.label.listener.ParserListener;
import com.sh_rw.label.utils.HexUtils;
import com.shrw.duke.serialport.SerialPortManager;
import com.shrw.duke.serialport.listener.PortDataListener;
import com.shrw.duke.serialport.port.AbsPort;
import com.shrw.duke.serialport.port.ShengAbsPort;
import com.shrw.duke.serialport.port.p.HanDePort;
import com.shrw.duke.serialport.port.p.IData95vPort;

import java.io.IOException;

public class MainActivity extends AppCompatActivity {
    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
//            super.handleMessage(msg);
            switch (msg.what) {
                case 0x01:
                    //不解析
                case 0x02:
                    //解析
                    mDisplay.append((String) msg.obj+"\n");
//                    BaseLabel label = (BaseLabel) msg.obj;
//                    mDisplay.append(label.toString());
                    break;
                default:
                    break;
            }

        }
    };
    private EditText mDisplay, mInput;
    private Button mOpen, mClose,mClear;
    private CheckBox mCheck;
    private LabelPerser mLabelPerser = null;
//    private AbsPort mPort = null;
    private SerialPortManager mSerialPortManager;

    private boolean isParser;
    private Message msg;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mDisplay = findViewById(R.id.et_display);
        mInput = findViewById(R.id.et_input);
        mOpen = findViewById(R.id.btn_open);
        mClose = findViewById(R.id.btn_close);
        mClear = findViewById(R.id.btn_clear);
        mCheck = findViewById(R.id.cb_check);

        mLabelPerser = new LabelPerser.Builder()
                .setRules(new BlackHandler())
                .setParserListener(new ParserListener<BaseLabel>() {
                    @Override
                    public void onLabelCallback(int cmd, BaseLabel baseLabel) {
                        msg = new Message();
                        msg.what = 0x02;
                        msg.obj = baseLabel.toString();
                        handler.sendMessage(msg);
                    }

                    @Override
                    public void onRFCallback(RFBean rfBean) {

                    }

                    @Override
                    public void onBaseArgCallback(BasicParam basicParam) {

                    }

                    @Override
                    public void onSuccess() {

                    }

                    @Override
                    public void onFail() {

                    }

                    @Override
                    public void onError(Exception o) {

                    }
                })
                .build();


        mSerialPortManager = SerialPortManager.getInstance();
        mSerialPortManager.initPort(new HanDePort());
        mSerialPortManager.setPortDataListener(new PortDataListener() {
            @Override
            public void onPortData(byte[] portData) {
                if (isParser&&mLabelPerser!= null){
                    mLabelPerser.analysis(portData);
                }else {
                    msg = new Message();
                    msg.what = 0x01;
                    msg.obj = HexUtils.toHexString(portData,portData.length).toString();
                    handler.sendMessage(msg);
                }
            }
        });

        mOpen.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                isParser = mCheck.isChecked();
                try {
                    open();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });

        mClose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                isParser = mCheck.isChecked();
                close();
            }
        });

        mClear.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mDisplay.setText("");
            }
        });

    }

    private void open() throws IOException {
        if (!mSerialPortManager.isOpen()) {
            mSerialPortManager.startReadPort();
        }
    }

    private void close() {
        if (mSerialPortManager.isOpen()) {
            mSerialPortManager.stopReadPort();
            mSerialPortManager.onDestroy();
        }
    }
}
